#include "EditorPageAreaModel.h"
#include "EditorPageAreaManagerModel.h"
#include "Window/EditorWindowGroupModel.h"
#include "Pages/EditorPageModel.h"

void REditorPageAreaModel::FirstTimeInitializeObject()
{
    Super::FirstTimeInitializeObject();
}

int64_t REditorPageAreaModel::GetPageNum() const
{
    return Pages.Num();
}

TSharedObjectPtr<REditorPageModel> REditorPageAreaModel::GetPage(int64_t Index) const
{
    return Pages[Index];
}

TSharedObjectPtr<REditorPageAreaManagerModel> REditorPageAreaModel::GetManager() const
{
    return GetOwner();
}

TSharedObjectPtr<REditorSplitableLayoutLeafNode> REditorPageAreaModel::GetLayoutNode() const
{
    return LayoutNode;
}

// int64_t REditorPageAreaModel::GetSubAreaNum() const
// {
//     return SubAreas.Num();
// }

// TSharedObjectPtr<REditorPageAreaModel> REditorPageAreaModel::GetSubArea(int64_t Index) const
// {
//     return SubAreas[Index];
// }

// float REditorPageAreaModel::GetSubAreaSizeWeight(int64_t Index) const
// {
//     return SubAreaSizes[Index];
// }


// bool REditorPageAreaModel::IsSubAreaVertical() const
// {
//     return bSubAreaVertical;
// }

// TSharedObjectPtr<REditorPageAreaManagerModel> REditorPageAreaModel::GetRootArea() const
// {
//     if(IsRoot())
//     {
//         return this;
//     }
//     return ParentArea->GetRootArea();
// }

// bool REditorPageAreaModel::IsRoot() const
// {
//     return this->GetClass()==REditorPageAreaManagerModel::StaticClass();
// }

bool REditorPageAreaModel::IsFocusedInTree() const
{
    return GetManager()->GetFocusedPageArea()==this;
}

// void REditorPageAreaModel::FindAreaToFocus()
// {
//     if(IsFocusedInTree())
//     {
//         return ;
//     }

//     if(!SubAreas.Empty())
//     {
//         SubAreas[0]->FindAreaToFocus();
//         return ;
//     }

//     if(!Pages.Empty())
//     {
//         GetRootArea()->FocusPageArea(this);
//     }
// }


int64_t REditorPageAreaModel::FindPage(const CString& PageID) const
{
    //CString PageID=FilePath.ToString();
    int64_t PageNum= Pages.Num();
    for(int64_t Index=0;Index<PageNum;Index++)
    {
        if(Pages[Index]->GetFileID() == PageID)
        {
            return Index;
        }
    }
    return -1;
}

int64_t REditorPageAreaModel::GetFrontPageIndex() const
{
    return FrontPageIndex;
}

TSharedObjectPtr<REditorPageModel> REditorPageAreaModel::GetFrontPage() const
{
    if(FrontPageIndex>=0)
    {
        return Pages[FrontPageIndex];
    }
    return nullptr;
}

void REditorPageAreaModel::BringPageFront(int64_t Index)
{
    FrontPageIndex= Index;

    if(IsFocusedInTree())
    {
        GetManager()->NofifyCurrentPageChanged();
    }
}

void REditorPageAreaModel::OpenSearchTextDialog()
{
    bShowingFindAndReplaceDialog= true;
    bShowingReplaceWidget=false;
}

void REditorPageAreaModel::OpenReplaceTextDialog()
{
    bShowingFindAndReplaceDialog= true;
    bShowingReplaceWidget=true;
}

bool REditorPageAreaModel::IsShowingFindAndReplaceDialog() const
{
    return bShowingFindAndReplaceDialog;
}

void REditorPageAreaModel::CloseFindAndReplaceDialog()
{
    bShowingFindAndReplaceDialog= false;
}

bool REditorPageAreaModel::IsShowingReplaceWidget() const
{
    return bShowingReplaceWidget;
}

void REditorPageAreaModel::ToggleReplaceWidget()
{
    bShowingReplaceWidget= !bShowingReplaceWidget;
}
